package net.easyjava.framework.log.file;

import net.easyjava.framework.utils.ProjectProperties;
import net.easyjava.tools.date.DateConstants;
import net.easyjava.tools.date.DateTools;
import net.easyjava.tools.io.file.FileTools;
import net.easyjava.tools.lang.StringTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created by wxnacy on 16/8/7.
 */
public class FileLogger {
    static Logger logger = LoggerFactory.getLogger(FileLogger.class);
    private Class clazz;
    private String path;
    /**
     * 构造单例
     */
    private volatile static FileLogger singleton;
    private FileLogger(final Class clazz){
        this.clazz = clazz;
        StringBuffer sb = new StringBuffer();
        String root = ProjectProperties.getInstance().getProperty("log.path");
        if(!StringTools.isEmpty(root)){
            sb.append(root);
            boolean flag = root.endsWith("/");
            if(!flag){
                sb.append("/");
            }
            sb.append("%s");
            sb.append("/").append("%s");
            this.path =  sb.toString();
        }
    }
    public static FileLogger getInstance(final Class clazz) {
        if (singleton == null) {
            synchronized (FileLogger.class) {
                if (singleton == null) {
                    singleton = new FileLogger(clazz);
                }
            }
        }
        return singleton;
    }


    protected String init(String root) throws IOException{
        if(this.path == null){
            throw new NullPointerException("log.path can't be null");
        }
        String fileName = this.clazz.getSimpleName();
        StringBuffer sb = new StringBuffer();
        if(StringTools.isEmpty(root)){
            sb.append(String.format(this.path,fileName,fileName));
        }else{
            sb.append(String.format(this.path,root,root));
        }
        return sb.toString();
    }

    protected String initPath(String root) throws IOException {
        String filePath = init(root);
        StringBuffer sb = new StringBuffer();
        sb.append(filePath);
        sb.append("-");
        sb.append(DateTools.format(System.currentTimeMillis(),DateConstants.DATE_TO_DAY));
        sb.append(".log");
        return sb.toString();
    }


    /**
     * 将日志吸入文件
     * @param content
     */
    public void write(String content) {
        write(null,content);
    }
    public void write(String root,String content) {
        try {
            String filePath = initPath(root);
            FileTools.write(filePath,content + "\n");
        } catch (IOException e) {
            logger.error("写日志失败 {}",content);
            logger.error(e.getMessage(),e.fillInStackTrace());
        }
    }

    /**
     * 带有日志格式的输出文件
     * @param content
     */
    public void writeWithTime(String content) {
        writeWithTime(null,content);
    }
    public void writeWithTime(String root,String content) {
        String time = DateTools.format(System.currentTimeMillis(), DateConstants.DATE_TIME_SSS_FORMAT);
        String cont = String.format("%s\t%s",time,content);
        write(root, cont);
    }


}
